package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.dl7.recycler.listener.OnRecyclerViewItemLongClickListener;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencySaveCheck1ListAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.SaveCheckLeaderCopyListBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

/**
 * Created by K on 2018/9/14.
 */
public class EmergencySaveCheck1Activity extends CommonListActivity {
    private List<SaveCheckLeaderCopyListBean> saveCheckLeaderBeanList;
    private String orgId = "";

    @Override
    public void initView() {
        super.initView();
    }


    @Override
    public void initTitle() {
        super.initTitle();
        iv_right_title.setVisibility(View.VISIBLE);
        iv_right_title.setImageDrawable(getResources().getDrawable(R.mipmap.add_white));
    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new EmergencySaveCheck1ListAdapter(this);
    }

    @Override
    protected String initTitleName() {
        return ConsTitles.EMERGENCY_SAVE_CHECK_1;
    }

    @Override
    public void configAdapter() {
        super.configAdapter();


        Intent intent = getIntent();
        if (null != intent) {
            orgId = intent.getStringExtra("id");
            if (!TextUtils.isEmpty(orgId)){
                requestData(orgId);
            }else {
                requestData(UserManager.getUser().bmcode);
            }
        }
    }

    @Override
    public void setListener() {
        super.setListener();
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                // startActivity(EmergencyEvent2Activity.class);
            }
        });


        iv_right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(EmergencySaveCheck1Activity.this, EmergencySaveZG1Activity.class);
                Intent intent = new Intent(EmergencySaveCheck1Activity.this, EmergencySaveCheckAddActivity.class);
//                intent.putExtra("id", orgId);
                startActivity(intent);
            }
        });

        mAdapter.setOnItemLongClickListener(new OnRecyclerViewItemLongClickListener() {
            @Override
            public boolean onItemLongClick(View view, int position) {
                //长按删除
                SaveCheckLeaderCopyListBean adviceListBean = saveCheckLeaderBeanList.get(position);

                deleteData(adviceListBean.getId());

                return false;
            }
        });


    }

    private void deleteData(String id) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_SAVE_CHECK_DELETE);
        sender.requestPost("id", id, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {

                requestData(orgId);
            }
        });
    }

    private void requestData(String orgId) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_SAVE_CHECK_LEADER);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", orgId);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                saveCheckLeaderBeanList = GsonUtil.jsonToList(listArray.toString(), SaveCheckLeaderCopyListBean.class);
                mAdapter.updateItems(saveCheckLeaderBeanList);
            }
        });
    }


}
